<template>
<div class="comment_placeholder">
  <div
      class="comment_cover"
      v-if="isActive"
      @click="isActive=false"
    ></div>
  <div class="comment">
    <!-- 形态1. 简单的展示 -->
    <div class="type1" v-show="!isActive" >
      <input type="text"  placeholder="写跟帖" @focus="inputFn" v-model.trim="content">
      <span class="icon-group">
        <router-link 
          :to="`/comment/${article.id}`" 
          class="iconfont iconpinglun-"></router-link>
        <em>{{article.comment_length}}</em>
      </span>
       <!-- 收藏按钮 -->
      <span class="icon-group" @click="collectFn" >
        <i class="iconfont iconshoucang" :class="{star:article.has_star}"></i>
      </span>
      <span class="icon-group">
        <i class="iconfont iconfenxiang"></i>
      </span>
    </div>
    <!-- 形态2. 文本域，用户课室输入 -->
    <div class="type2" v-show="isActive" >
      <!-- Vant 也有组件 -->
      <textarea
        class="textarea"
        placeholder="回复: @火星网友"
        ref="textarea"
        v-model.trim="content"
      ></textarea>
      <div class="btn-group">
        <span class="btn" @click="isActive=false">取消</span>
        <span class="btn" @click="sendBtn">发送</span>
      </div>
    </div>
  </div>
        
</div>
  
</template>

<script>
import { publishPostCommentAPI , updatePostStarByIdAPI } from "../apis/news";
export default {
    props: {
        article:Object,
        commentObj:Object,
    },
    data() {
      return {
        isActive: false,
        content:'',
      };
    },
    watch:{
      commentObj:{
        deep:true,
        handler(){
          if(this.commentObj){
            this.content='@'+this.commentObj.user.nickname+""
          }
        },
        content(){
          if(this.content===''){
            this.$emit('reset')
          }
        }
      }
    },
    methods: {
        async collectFn() {
          // console.log("实现文章收藏", this.article.id);
          // 调用接口
          const res = await updatePostStarByIdAPI(this.article.id);
          console.log(res);
          const {message}=res.data
          if(res.data.message==="收藏成功"){
              this.$toast.success(message);
          }else{
            this.$toast.fail(message)
          }
          this.article.has_star=!this.article.has_star
        },
        inputFn(){
            this.isActive=true
            // 需要等待视图更新后，才能获取到焦点
            this.$nextTick(() => {
              this.$refs.textarea.focus();
          });
        },
          // 点击发送按钮 - 评论当前文章
        async sendBtn() {
          if(this.content==="")return this.$toast.fail("请输入内容");
          // 调用发表评论的接口
          const res = await publishPostCommentAPI(this.article.id, {
            content: this.content,
            
            // 两种情况：情况1：回复某个评论(有评论id)      情况2：直接回复文章(没有评论id)
            parent_id:this.commentObj?this.commentObj.id:null
          });
          // console.log(res);
          // 发表成功后的提示
          this.$toast.success(res.data.message);
          // 清空文本域
          this.content = "";
          // 切换回形态1
          this.isActive = false;
          // 由于评论列表的数据是在父组件上，子传父
          this.$emit("update");
        }
    },
  
};
</script>

<style lang="less" scoped>
// 定位的父盒子负责占位，定位的底部评论盖住了页面的内容
.comment_placeholder {
  height: 100px;
}
.comment_cover {
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
}
.comment {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #f2f2f2;
  z-index: 11;
}
.star {
  color: rgb(236, 116, 4);
}
.type1 {
  display: flex;
  height: 50px;
  align-items: center;
  padding: 0 10px;
  input {
    flex: 1;
    height: 30px;
    border-radius: 15px;
    background-color: #d7d7d7;
    border: none;
    padding: 0 10px;
    margin: 10px;
  }
  .icon-group {
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    .iconfont {
      font-size: 20px;
    }
    em {
      background-color: red;
      color: #fff;
      position: absolute;
      right: 0px;
      top: 0;
      font-size: 12px;
      border-radius: 15px;
      padding: 0 2px;
    }
  }
}
.type2 {
  display: flex;
  padding: 10px;
  .textarea {
    height: 80px;
    background-color: #d7d7d7;
    border-radius: 5px;
    flex: 1;
    padding: 5px;
  }

  .btn-group {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .btn {
      width: 60px;
      height: 25px;
      background-color: red;
      color: #fff;
      border-radius: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>